package main.java.easy;

/**
 * @Author: 郑国荣
 * @Date: 2023-07-23-1:06
 * @Description: 二进制相加+1
 */
public class AddBinary {
    public static String addBinary(String a, String b){
        StringBuilder sb = new StringBuilder();
        int n = Math.max(a.length(),b.length()),sum = 0;
        for(int i=0;i<n;++i){
            if(i<a.length()) sum += a.charAt(a.length()-i-1)-'0';
            if(i<b.length()) sum += b.charAt(b.length()-i-1)-'0';
            sb.append((char)(sum%2+'0'));
            sum /= 2;
        }

        if(sum >0){
            sb.append('1');
        }
        sb.reverse();
        return sb.toString();
    }

    public static void main(String[] args) {
        String s1 = "1011";
        String s2 = "1011";
        System.out.println(addBinary(s1, s2));
    }
}
